/**
 * 地理位置API测试文件
 * 
 * 使用方法：
 * 1. 配置好 config.default.js 中的地理位置API
 * 2. 运行: node test/location-api-test.js
 */

const config = require('../config/config.default.js')();

async function testLocationAPI() {
  console.log('📍 开始测试地理位置API配置...\n');
  
  // 检查配置
  const provider = config.location?.provider || 'ip';
  const amapKey = config.location?.amapKey || 'your_amap_key';
  const baiduKey = config.location?.baiduKey || 'your_baidu_key';
  const tencentKey = config.location?.tencentKey || 'your_tencent_key';
  
  console.log('📋 当前配置:');
  console.log(`Provider: ${provider}`);
  console.log(`高德Key: ${amapKey === 'your_amap_key' ? '未配置' : '已配置'}`);
  console.log(`百度Key: ${baiduKey === 'your_baidu_key' ? '未配置' : '已配置'}`);
  console.log(`腾讯Key: ${tencentKey === 'your_tencent_key' ? '未配置' : '已配置'}\n`);
  
  // 测试不同的API配置
  console.log('🔍 API配置建议:\n');
  
  if (provider === 'amap' && amapKey !== 'your_amap_key') {
    console.log('✅ 高德地图API已配置');
    console.log('📡 地理编码请求示例:');
    console.log(`https://restapi.amap.com/v3/geocode/geo?address=北京市&key=${amapKey}`);
    console.log('📡 逆地理编码请求示例:');
    console.log(`https://restapi.amap.com/v3/geocode/regeo?location=116.4074,39.9042&key=${amapKey}`);
    console.log('📡 IP定位请求示例:');
    console.log(`https://restapi.amap.com/v3/ip?ip=8.8.8.8&key=${amapKey}\n`);
  } else if (provider === 'amap') {
    console.log('⚠️ 高德地图API未配置');
    console.log('📝 配置方法:');
    console.log('1. 访问 https://lbs.amap.com/ 注册账号');
    console.log('2. 创建应用获取API Key');
    console.log('3. 在 config/config.default.js 中配置 amapKey\n');
  }
  
  if (provider === 'baidu' && baiduKey !== 'your_baidu_key') {
    console.log('✅ 百度地图API已配置');
    console.log('📡 地理编码请求示例:');
    console.log(`https://api.map.baidu.com/geocoding/v3/?address=北京市&output=json&ak=${baiduKey}`);
  } else if (provider === 'baidu') {
    console.log('⚠️ 百度地图API未配置');
    console.log('📝 配置方法:');
    console.log('1. 访问 https://lbsyun.baidu.com/ 注册账号');
    console.log('2. 创建应用获取AK');
    console.log('3. 在 config/config.default.js 中配置 baiduKey\n');
  }
  
  if (provider === 'tencent' && tencentKey !== 'your_tencent_key') {
    console.log('✅ 腾讯地图API已配置');
    console.log('📡 地理编码请求示例:');
    console.log(`https://apis.map.qq.com/ws/geocoder/v1/?address=北京市&key=${tencentKey}`);
  } else if (provider === 'tencent') {
    console.log('⚠️ 腾讯地图API未配置');
    console.log('📝 配置方法:');
    console.log('1. 访问 https://lbs.qq.com/ 注册账号');
    console.log('2. 创建应用获取Key');
    console.log('3. 在 config/config.default.js 中配置 tencentKey\n');
  }
  
  if (provider === 'ip') {
    console.log('✅ 使用免费IP定位服务');
    console.log('📡 IP定位请求示例:');
    console.log('http://ip-api.com/json/8.8.8.8?lang=zh-CN');
    console.log('💡 提示: IP定位精度较低，建议配置地图API获得更准确的位置信息\n');
  }
  
  // API接口测试示例
  console.log('🚀 API接口使用示例:\n');
  
  console.log('1. 获取当前位置:');
  console.log('GET /api/v1/location/current?lat=39.9042&lng=116.4074\n');
  
  console.log('2. 地理编码（地址转坐标）:');
  console.log('POST /api/v1/location/geocode');
  console.log('{ "address": "北京市朝阳区" }\n');
  
  console.log('3. 逆地理编码（坐标转地址）:');
  console.log('POST /api/v1/location/reverse');
  console.log('{ "lat": 39.9042, "lng": 116.4074 }\n');
  
  console.log('4. IP定位:');
  console.log('GET /api/v1/location/ip?ip=8.8.8.8\n');
  
  console.log('5. 获取城市列表:');
  console.log('GET /api/v1/location/cities\n');
  
  console.log('6. 搜索地点:');
  console.log('GET /api/v1/location/search?keyword=天安门&city=北京\n');
  
  // 推荐配置
  console.log('💡 推荐配置:\n');
  console.log('对于国内用户，推荐使用高德地图API:');
  console.log('- 数据最准确');
  console.log('- 免费额度大（每日30万次）');
  console.log('- 中文支持完善');
  console.log('- 功能丰富\n');
  
  console.log('配置示例:');
  console.log('config.location = {');
  console.log('  provider: "amap",');
  console.log('  amapKey: "your_actual_amap_key"');
  console.log('};\n');
  
  console.log('✅ 地理位置API配置检查完成');
}

// 运行测试
if (require.main === module) {
  testLocationAPI();
}

module.exports = { testLocationAPI };
